package org.example;

import org.apache.ibatis.session.SqlSession;
import org.example.mapper.OrderDetailMapper;
import org.example.mapper.OrderMapper;
import org.example.mapper.ProductMapper;
import org.example.mapper.UserMapper;
import org.example.pojo.Order;
import org.example.pojo.OrderDetail;
import org.example.pojo.Product;
import org.example.pojo.User;
import org.example.utils.MyBatisUtils;
import org.junit.Test;

import java.util.List;

public class Main {
    /**
     * 单个查询用户
     */
    @Test
    public void findUserById() {
        SqlSession session = MyBatisUtils.getSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        int i = 1;
        User user = userMapper.findUserById(i);
        System.out.println(user);
    }

    /**
     * 批量查询用户
     */
    @Test
    public void findByArray() {
        SqlSession session = MyBatisUtils.getSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        int[] args = {2, 3};
        List<User> users = userMapper.findByArray(args);
        for (User user : users) {
            System.out.println(user);
        }
    }

    /**
     * 根据用户id删除用户
     */
//    @Test
    public void deleteUserById() {
        SqlSession session = MyBatisUtils.getSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        int i = userMapper.deleteUserById(1);
        if (i > 0) {
            System.out.println("删除成功！");
        } else {
            System.out.println("删除失败！");
        }
    }

    /**
     * 更新用户信息
     */
    @Test
    public void updateUser() {
        SqlSession session = MyBatisUtils.getSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        User user = new User();
        user.setUsername("new_username");
        user.setPassword("new_password");
        user.setEmail("new_email@example.com");
        user.setId(1);
        int i = userMapper.updateUser(user);
        if (i > 0) {
            System.out.println("修改成功！");
        } else {
            System.out.println("修改失败！");
        }
    }

    /**
     * 根据用户id查询订单；一对多
     */
    @Test
    public void findOrdersByUserId() {
        SqlSession session = null;
        try {
            session = MyBatisUtils.getSession();
            UserMapper userMapper = session.getMapper(UserMapper.class);
            User user = userMapper.getUserWithOrders(1);
            System.out.println(user);
            // 如果查询操作不影响数据库状态，可以不提交事务，这里只是示例
            session.commit();
        } catch (Exception e) {
            // 打印详细错误信息
            e.printStackTrace();
            if (session!= null) {
                // 回滚事务（如果需要）
                session.rollback();
            }
        } finally {
            if (session!= null) {
                // 关闭数据库连接
                session.close();
            }
        }
    }

    /**
     * 商品增删改查操作
     */
    @Test
    public void products() {
        try (SqlSession sqlSession = MyBatisUtils.getSession()) {
            ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);
            // 测试插入操作
            Product newProduct = new Product();
            newProduct.setName("New Product");
            newProduct.setPrice(19.99);
            newProduct.setStock(50);
            productMapper.insertProduct(newProduct);
            System.out.println("新产品插入成功，产品ID为：" + newProduct.getId());
            // 测试查询操作
            Product retrievedProduct = productMapper.findProductById(newProduct.getId());
            System.out.println("查询到的产品信息：" + retrievedProduct);
            // 测试更新操作
            retrievedProduct.setName("Updated Product");
            retrievedProduct.setPrice(24.99);
            productMapper.updateProduct(retrievedProduct);
            System.out.println("产品更新成功");
            // 测试删除操作
            productMapper.deleteProductById(retrievedProduct.getId());
            System.out.println("产品删除成功");
            // 提交事务
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 订单增删改查操作
     */
    @Test
    public void orders() {
        try (SqlSession sqlSession = MyBatisUtils.getSession()) {
            OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
            // 测试插入操作
            Order newOrder = new Order();
            newOrder.setUser_id(1);
            newOrder.setOrder_date("2023-12-20");
            orderMapper.insertOrder(newOrder);
            System.out.println("新订单插入成功，订单ID为：" + newOrder.getId());
            // 测试查询操作
            Order retrievedOrder = orderMapper.getOrderById(newOrder.getId());
            System.out.println("查询到的订单信息：" + retrievedOrder);
            // 测试更新操作
            retrievedOrder.setOrder_date("2023-12-21");
            orderMapper.updateOrder(retrievedOrder);
            System.out.println("订单更新成功");
            // 测试删除操作
            orderMapper.deleteOrderById(retrievedOrder.getId());
            System.out.println("订单删除成功");
            // 提交事务
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}